﻿#include "framework.h"
#include "Block.h"

// 全局变量
extern ColorGrid gExistentBlocks[GAME_BOARD_GRID_COUNT_V][GAME_BOARD_GRID_COUNT_H];

/**
 * 创建方块。
 * @return 方块
 */
Block* New_Block() {
    Block* block = (Block*)calloc(1, sizeof(Block));
    int red = rand() % 256;
    int green = rand() % 256;
    int blue = rand() % 256;

    // 初始化数据
    block->mColor = RGB(red, green, blue);
    block->mLine = 3;
    block->mRow = -1;

    // 初始化函数
    block->draw = Block_Draw;
    block->enterGameBoard = Block_EnterGameboard;
    block->eraseRect = Block_EraseRect;

    return block;
}

/**
 * 绘制。
 * @param block 方块
 */
void Block_Draw(Block* block) {
    int i = 0, j = 0;
    RECT rect = { 0 };

    for (i = 0; i < BLOCK_GRID_COUNT_H; i++) {
        for (j = 0; j < BLOCK_GRID_COUNT_V; j++) {
            if (!block->mBlocks[i][j].mUsed) {
                continue;
            }

            rect.left = GRID_SIZE * (block->mLine + j);
            rect.top = GRID_SIZE * (block->mRow + i);
            rect.right = rect.left + GRID_SIZE;
            rect.bottom = rect.top + GRID_SIZE;
            SetBkColor(block->mDrawer, block->mColor);
            ExtTextOut(block->mDrawer, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
            DrawEdge(block->mDrawer, &rect, EDGE_RAISED, BF_RECT);
        }
    }

    InvalidateRect(block->mGameBoard, NULL, FALSE);
}

/**
 * 进入游戏区域。
 * @param block             方块
 * @param gameBoard         游戏区域
 * @param drawer            绘图者
 */
void Block_EnterGameboard(Block* block, HWND gameBoard, HDC drawer) {
    block->mGameBoard = gameBoard;
    block->mDrawer = drawer;
    block->draw(block);
}

/**
* 进入预览区域。
* @param block  方块
* @param drawer 绘图者
*/
void Block_EnterPreview(Block* block, HDC drawer) {
    int x = (PREVIEW_WIDTH - GRID_SIZE * 3) / 2;
    int y = PREVIEW_HEIGHT / 2 - GRID_SIZE * 2;
    RECT rect = { 0 };
    int i = 0, j = 0;

    // 遍历所有格，跳过无效的，画有效的
    for (i = 0; i < BLOCK_GRID_COUNT_H; i++) {
        for (j = 0; j < BLOCK_GRID_COUNT_V; j++) {
            if (!block->mBlocks[i][j].mUsed) {
                continue;
            }

            rect.left = x + GRID_SIZE * j;
            rect.top = y + GRID_SIZE * i;
            rect.right = x + GRID_SIZE * (j + 1);
            rect.bottom = y + GRID_SIZE * (i + 1);

            SetBkColor(drawer, block->mColor);
            ExtTextOut(drawer, 0, 0, ETO_OPAQUE, &rect, NULL, 0, NULL);
            DrawEdge(drawer, &rect, EDGE_RAISED, BF_RECT);
        }
    }
}

/**
 * 擦除矩形。
 * @param block 方块
 * @param rect  矩形
 */
void Block_EraseRect(Block* block, RECT* rect) {
    SetBkColor(block->mDrawer, GAME_BOARD_COLOR);
    ExtTextOut(block->mDrawer, 0, 0, ETO_OPAQUE, rect, NULL, 0, NULL);
}

/**
 * 下落。
 * @param block 方块
 * @return 总是 TRUE
 */
BOOL Block_MoveDown(Block* block) {
    block->erase(block);
    block->mRow++;
    block->draw(block);

    return TRUE;
}

/**
 * 左移。
 * @param block 方块
 */
void Block_MoveLeft(Block* block) {
    block->erase(block);
    block->mLine--;
    block->draw(block);
}

/**
 * 右移。
 * @param block 方块
 */
void Block_MoveRight(Block* block) {
    block->erase(block);
    block->mLine++;
    block->draw(block);
}
